package ch01.BitmapCollision
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import tools.HitTest;
	import tools.Triangle;
	
	public class HitTest6 extends Sprite
	{
		private var tx:Number, ty:Number;
		private var bmd1:BitmapData, bmd2:BitmapData;
		//        private var rect:Rectangle, bc1:BitmapData, bc2:BitmapData, bc:Object;
		private var bc1:BitmapData, bc2:BitmapData;
		private var compareObj:Object;
		private var tri1:Triangle;
		private var tri2:Triangle;
		private var isHit:Boolean = false;
		private var crossRect:Shape;
		
		public function HitTest6()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			setTimeout(callLaterHandler, 500);
		}
		
		/**
		 * 该函数需要延时处理，否则在IE6下，stage.stageWidth和stage.stageHeight为0，BitmapData创建会出现异常
		 * */
		private function callLaterHandler(event:Event = null):void
		{
			// make two stars, add to stage
			tri1 = new Triangle(50);
			tri1.rotation = 30;
			tri1.x = 100;
			addChild(tri1);
			tri2 = new Triangle(50);
			tri2.x = 200;
			tri2.y = 200;
			addChild(tri2);
			
			//让mc1元件有按钮模式, 并去掉mc3的鼠标事件
			tri1.buttonMode = true;
			//            mc3.mouseEnabled = false;
			
			//侦听mc1按下，以便拖拽
			tri1.addEventListener(MouseEvent.MOUSE_DOWN, mousedownHandler);
			
			//让mc2停止在第一帧上，表示没有碰撞
			//            tri2.gotoAndStop(1);
			
			//取得mc1的bitmapdata数据
			bmd1 = new BitmapData(tri1.width, tri1.height, true, 0);
			bmd1.draw(tri1, null, new ColorTransform(125, 125, 125, 1));
			
			//取得mc2的bitmapdata数据
			bmd2 = new BitmapData(tri2.width, tri2.height, true, 0);
			bmd2.draw(tri2, null, new ColorTransform(0, 0, 0, 1));
			crossRect = new Shape();
			addChild(crossRect);
		}
		
		private function getMaxRect():void
		{
			//得到mc1,mc2的组合在一起的矩形，并生成两个相同新的bitmapdata,bc1,bc2，以便稍后相比较
			//            var bx:Number = (tri1.x > tri2.x) ? tri2.x : tri1.x;
			//            var by:Number = (tri1.y > tri2.y) ? tri2.y : tri1.y;
			var bw:Number = (tri1.x > tri2.x) ? Math.abs(tri1.x - tri2.x + tri1.width) : Math.abs(tri2.x - tri1.x + tri2.width);
			var bh:Number = (tri1.y > tri2.y) ? Math.abs(tri1.y - tri2.y + tri1.height) : Math.abs(tri2.y - tri1.y + tri2.height);
			
			bc1 = new BitmapData(bw, bh, true, 0);
			bc2 = bc1.clone();
		}
		
		private function compareRect():void
		{
			var g:Graphics = crossRect.graphics;
			//分别在得到的bc1,bc2中用不同的步骤画上mc1,mc2的bitmapdata
			bc1.copyPixels(bmd1, bmd1.rect, new Point(0, 0), null, null, true);
			bc1.copyPixels(bmd2, bmd2.rect, new Point(tri2.x - tri1.x, tri2.y - tri1.y), null, null, true);
			bc2.copyPixels(bmd2, bmd2.rect, new Point(tri2.x - tri1.x, tri2.y - tri1.y), null, null, true);
			bc2.copyPixels(bmd1, bmd1.rect, new Point(0, 0), null, null, true);
			//清除之前的compareObj,并且比较现在的两个大的bitmapdata，得到新的compareObj
			if (compareObj is BitmapData)
			{
				compareObj.dispose();
			}
			compareObj = bc1.compare(bc2);
			
			//画好后，比较bc1,bc2,如果bc1,bc2有不同的RGB 值,将会返回一个新的bitmapdata,否则返回0
			if (compareObj is BitmapData)
			{
				//显示这个比较后的相交的bitmapdata,如果要显示相交区域，把下面的注释去掉即可。
				//                var bmp:Bitmap = new Bitmap(compareObj as BitmapData);
				//                bmp.x = tri1.x;
				//                bmp.y = tri1.y;
				//                this.addChild(bmp);
				
				//得到相交部分的矩形,并用mc3来显示
				var r:Rectangle = compareObj.getColorBoundsRect(0xFFFFFFFF, 0xFF000000, false);
				trace(r);
				g.clear();
				g.lineStyle(0x000000);
				g.beginFill(0xFF0000, 0.5);
				g.drawRect(tri1.x + r.x, tri1.y + r.y, r.width, r.height);
				g.endFill();
				
				//                mc3.x = tri1.x + r.x;
				//                mc3.y = tri1.y + r.y;
				//                mc3.width = r.width;
				//                mc3.height = r.height;
				
				//                txt.text = "有碰撞, 相对于mc1的位置是: x:" + r.x + " y:" + r.y + " width:" + r.width + " height:" + r.height;
			}
			else
			{
				g.clear();
				//mc3复原
				//                mc3.x = -35;
				//                mc3.y = 35;
				//                mc3.width = mc3.height = 30;
				//                txt.text = "无碰撞";
			}
		}
		
		private function removeAllBitmapData():void
		{
			bc1.dispose();
			bc2.dispose();
			bc1 = null;
			bc2 = null;
		}
		
		private function mousedownHandler(e:MouseEvent):void
		{
			//tx,ty为鼠标按下时，mc1的坐标相对原点的位置
			tx = e.stageX - tri1.x;
			ty = e.stageY - tri1.y;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseupHandler); //侦听stage的鼠标事件，以便拖拽
		}
		
		private function mousemoveHandler(e:MouseEvent):void
		{
			tri1.x = e.stageX - tx;
			tri1.y = e.stageY - ty;
			getMaxRect(); //得到两个大的bitmapdata
			compareRect(); //比较这两个大的bitmapdata
			removeAllBitmapData(); //移除这两个大的bitmapdata
		}
		
		private function mouseupHandler(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mousemoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseupHandler); //之前有侦听这两个事件，当松开鼠标时，需要除去这两个事件
		}
	}
}